﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ActivateAccount : System.Web.UI.Page
{
    ILE_Global ileGlobal = new ILE_Global();
    ILE_Config ileConfig = new ILE_Config();
    UsersBLL usersLogic = new UsersBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string Email = String.Empty;
            string ActivationCode = String.Empty;

            try
            {
                Email = Request.QueryString["Email"];
                ActivationCode = Request.QueryString["Code"];
            }
            catch
            {

            }

            txtEmail.Text = Email;
            txtResendEmail.Text = Email;
            txtActivationCode.Text = ActivationCode;
        }
    }

    protected void btnActivate_Click(object sender, EventArgs e)
    {
        string Email = txtEmail.Text;
        string ActivationCode = txtActivationCode.Text;

        bool result = usersLogic.ActivateNonInstituteUser(Email, ActivationCode);
        Users.UsersRow user = usersLogic.GetUserByUsername(Email);
        bool result2 = usersLogic.UpdateUserByUsername(user.AccountType, user.FirstName, user.LastName, String.Empty, user.Email, true, user.Year, Email);

        if (result && result2)
        {
            // *SteveMsg
            string _msg = "Successfully activated the account using " + Email + ".";
            ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", _msg, 1, "Login.aspx?TabIndex=1");
        }
        else
        {
            // *SteveMsg
            string _msg = "Account failed to activate. Please try again.";
            ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
        }
    }

    protected void btnResendActivationCode_Click(object sender, EventArgs e)
    {
        string Email = txtResendEmail.Text;

        Users.UsersRow user = usersLogic.GetUserByUsername(Email);

        int UserID = 0;
        try
        {
            UserID = user.UserID;
        }
        catch
        {
            // *SteveMsg
            ModalPopupMessage.ShowModalPopup("ILE Message", "The Email Address is not registered.", 2);
            return;
        }

        Users.Users_NonInstitute_ActivationRow activation = usersLogic.GetActivationCode(UserID);

        System.Net.Mail.MailAddressCollection receivers = new System.Net.Mail.MailAddressCollection();
        receivers.Add(Email);

        // *SteveMsg
        string _EmailBody = "You have made an account for Information Literacy Education (ILE). <br /><a href=''>Click here to Activate your account</a><br />Activation Code : " + activation.ActivationCode;
        try
        {
            ileGlobal.SendEmail(receivers, ileConfig.getDefaultEmailSenderAddress().ToString(), "ILE Account Activation",
                _EmailBody);
        }
        catch
        {
            // *SteveMsg
            ModalPopupMessage.ShowModalPopup("ILE Message", "Email failed to send.", 2);
            return;
        }

        // *SteveMsg
        ModalPopupMessage.ShowModalPopup("ILE Message", "An email has been sent to the address provided.", 1);
    }

}
